package com.yu.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yu.model.dto.question.JudgeCase;
import com.yu.model.dto.question.JudgeConfig;
import com.yu.judge.model.JudgeInfo;
import com.yu.model.entity.Question;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class JavaJudgeStrategy implements JudgeStrategy {

    /**
     *
     * 执行判题
     * 此列为java执行列
     * 后期可能有不同语言执行
     *----
     * 目前为止为测试阶段  后面应该补充执行信息给response 的message值作为结果返回
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {


        List<String> output = judgeContext.getOutput();
        List<String> input = judgeContext.getInput();

        Question question = judgeContext.getQuestion();

        List<JudgeCase> inputList = judgeContext.getInputList();

        String judgeConfig = question.getJudgeConfig();
        //转换成json数组
        JudgeConfig config = JSONUtil.toBean(judgeConfig, JudgeConfig.class);
        Long memory = config.getMemoryLimit();
        Long timeLimit = config.getTimeLimit();
        Long stackLimit = config.getStackLimit();



        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        Long judgeInfoTime = judgeInfo.getTime();
        Long judgeInfoMemory = judgeInfo.getMemory();
        String message = judgeInfo.getMessage();
        Long judgeInfoStackLimit = judgeInfo.getStackLimit();


        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setTime(judgeInfoTime);
        judgeInfoResponse.setMemory(judgeInfoMemory);
        judgeInfoResponse.setMessage("测试----目前为空");
        judgeInfoResponse.setStackLimit(judgeInfoStackLimit);

//        List<String> output = excuteResponse.getOutput();  //执行后的答案
        if(output.size()!=input.size()){
            return null;
        }
        for (int i = 0; i < inputList.size(); i++) {
            JudgeCase judgeCase1=inputList.get(i);
            if(!judgeCase1.getOutput().equals(output.get(i)))
            {
                log.info("有示例不符合预期");
                return judgeInfoResponse;
            }
        }
        //获取题目的config

        if(memory<judgeInfoMemory){
            log.info("内存不符合预期");
            return judgeInfoResponse;
        }
        if(timeLimit<judgeInfoTime){
            log.info("时间限制不符合预期");
            return judgeInfoResponse;
        }
        if(stackLimit<judgeInfoStackLimit){
            log.info("栈内存不符合预期");
            return judgeInfoResponse;
        }



        return judgeInfoResponse;
    }
}
